﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 设置管理器
    /// </summary>
    /// <remarks>
    /// <para>对应 SketchUpAPI 中的 SUOptionsManagerRef </para>
    /// 包含四个页面：PageOptions, SlideshowOptions, UnitsOptions and PrintOptions</remarks>
    public class OptionsProviderTable : Entity
    {
        #region API 函数
        /// <summary>
        /// 获取选项设置器个数
        /// </summary>
        /// <param name="opMgrRef"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUOptionsManagerGetNumOptionsProviders(IntPtr opMgrRef, out long num);

        /// <summary>
        /// 通过名字查找选项设置器
        /// </summary>
        /// <param name="managerRef"></param>
        /// <param name="name"></param>
        /// <param name="providerRef"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUOptionsManagerGetOptionsProviderByName(IntPtr managerRef, [MarshalAs(UnmanagedType.LPUTF8Str)] string name, out IntPtr providerRef);

        /// <summary>
        /// 查找全部选项设置器的名字
        /// </summary>
        /// <param name="opMgrRef"></param>
        /// <param name="len"></param>
        /// <param name="options_provider_names"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUOptionsManagerGetOptionsProviderNames(IntPtr opMgrRef, long len, IntPtr[] options_provider_names, out long count);
        #endregion

        /// <summary>
        /// 选项设置器个数
        /// </summary>
        public long Count
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUOptionsManagerGetNumOptionsProviders(Id, out long num));
                return num;
            }
        }

        /// <summary>
        /// 全部选项设置器的名字
        /// </summary>
        public List<string> OptionNames
        {
            get
            {
                if (Count == 0) return new List<string>();

                StringRef[] names = new StringRef[Count];
                for (int i = 0; i < names.Length; i++)
                {
                    names[i] = StringRef.Empty;
                }

                IntPtr[] ptrs = names.Select(ss => ss.Id).ToArray();
                SketchUpExceptionHelper.ThrowSUResult(SUOptionsManagerGetOptionsProviderNames(Id, Count, ptrs, out long returnCount));
                return names.Select(ss => ss.Dispose()).ToList();
            }
        }

        /// <summary>
        /// 获取单位设置器
        /// </summary>
        public OptionsProviderUnits UnitsOptions
        {
            get
            {
                return new OptionsProviderUnits(this[nameof(UnitsOptions)]);
            }
        }

        /// <summary>
        /// 通过名字获取选项设置器
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public OptionsProvider this[string name]
        {
            get
            {
                OptionsProvider op = new OptionsProvider();
                SketchUpExceptionHelper.ThrowSUResult(SUOptionsManagerGetOptionsProviderByName(Id, name, out op.Id));
                return op;
            }
        }

        internal OptionsProviderTable(IntPtr id)
        {
            Id = id;
        }
    }
}